﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VgoCardChange.Models.DAL;
using VgoCardChange.Models.Objects;

namespace VgoCardChange.Models
{
    public class UserModels
    {
        ConnectDB objData;
        #region UserLogin
        //function check user Login
        public bool CheckUserLogin(string UserName, string PassWord)
        {
            bool isHaveUser = false;
            ConnectDB objData = new ConnectDB();
            User user;
            string strSQL = "";
            try
            {
                strSQL = "select UserName, PassWord from TblUser where UserName = N'" + UserName + "'"
                                    + " and PassWord ='" + PassWord + "'";
                objData.ExeReader(strSQL);
                if (objData.dataRead.Read())
                {

                    string usname = objData.dataRead["UserName"].ToString();
                    string pwd = objData.dataRead["PassWord"].ToString();
                    user = new User();
                    user.UserName = usname;
                    user.PassWord = pwd;
                    if (user.UserName == UserName && user.PassWord == PassWord)
                    {
                        isHaveUser = true;
                    }
                    else
                        isHaveUser = false;
                }
                else isHaveUser = false;
            }
            catch (Exception ex)
            {
                Console.Write("Error: " + ex);
            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
            return isHaveUser;
        }
        #endregion

        #region change password
        public void ChangePassword(User Us)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_ChangePassword @PassWord=N'" + Us.PassWord + "'" + ",@UserName=N'" + Us.UserName + "'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally
            {
                con.CloseData();
                con = null;
            }

        }
        #endregion
        #region Check Password
        public bool CheckPassword(string PassWord, string UserName)
        {
            bool ok = false;
            ConnectDB objData = new ConnectDB();
            string strSQL = "";
            User Us;
            try
            {
                strSQL = "sp_CheckPassword @PassWord = N'" + PassWord + "'" + ",@UserName=N'" + UserName + "'";
                objData.ExeReader(strSQL);
                if (objData.dataRead.Read())
                {
                    Us = new User();
                    Us.PassWord = objData.dataRead["PassWord"].ToString();
                    if (Us.PassWord == PassWord)
                    {
                        ok = true;
                    }
                    else
                        ok = false;
                }
            }
            catch (Exception ex)
            {
                Console.Write("Error: " + ex);
            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
            return ok;
        }
        #endregion
        public List<User> GetUser()
        {
            List<User> listuser = new List<User>();
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "select UserID,UserName,FullName from TblUser";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    User Us = new User();
                    Us.UserID = int.Parse(con.dataRead["UserID"].ToString());
                    Us.UserName = con.dataRead["UserName"].ToString();
                    Us.FullName = con.dataRead["FullName"].ToString();
                    listuser.Add(Us);
                }
            }
            catch (Exception ex)
            { Console.Write(ex); }
            finally { con.CloseData(); con = null; }
            return listuser;
        }
        //Get List City
        #region getList city
        public List<City> GetAllCity() 
        {
            ConnectDB objData = new ConnectDB();
            List<City> lstCity = new List<City>();
            City objCity;
            string strSQl = "";
            try 
            {
                strSQl = "select [CityID], [CityName] from [TblCity]";
                objData.ExeReader(strSQl);
                while(objData.dataRead.Read())
                {
                    objCity = new City();
                    objCity.CityID = int.Parse(objData.dataRead["CityID"].ToString());
                    objCity.CityName = objData.dataRead["CityName"].ToString();
                    lstCity.Add(objCity);
                }
                return lstCity;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);    
                return new List<City>();
            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
        }
        #endregion
        //Get City By ID
        #region get City name
        public string GetCityName(string CityID) 
        {
            string CityName = "";
            ConnectDB objData = new ConnectDB();
            string strSQL = "";
            try 
            {
                strSQL = "select CityName from TblCity where CityID = '" + CityID +"'";
                objData.ExeReader(strSQL);
                while(objData.dataRead.Read())
                {
                    CityName = objData.dataRead["CityName"].ToString();
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
            return CityName;
        }
        #endregion
        //Function Register User 
        #region Register new User
        public void Register(User user)
        {
            objData = new ConnectDB();
            string strSQLInAddUser = "";
            try
            {
                //add user query
                strSQLInAddUser = "sp_addNewUser @UserName =N'" + user.UserName + "'"
                                               + ",@PassWord =N'" + user.PassWord + "'"
                                               + ",@FullName = N'" + user.FullName + "'"
                                               + ",@BirthDate ='" + user.BirthDate + "'"
                                               + ",@Gender =N'" + user.Gender + "'"
                                               + ",@Email =N'" + user.Email + "'"
                                               + ",@Address =N'" + user.Address + "'"
                                               + ",@PhoneNumber =" + user.PhoneNumber + ""
                                               + ",@IdentifyNumber=" + user.IdentifyNumber + ""
                                               + ",@DateSupplyID ='" + user.DateSupplyID + "'"
                                               + ",@PlaceSupplyID =N'" + user.PlaceSupplyID + "'"
                                               + ",@RoleID =" + user.RoleID + ""
                                               + ",@ServerID=" + user.ServerID + ""
                                               + ",@HomeTown=N'" + user.HomeTown + "'";
                //Bắt đầu insert
                objData.ExeNonQuery(strSQLInAddUser);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
        }
        #endregion
        //Function to get User ID
        #region get User ID
        public int GetUserID(string UserName)
        {
            int UserID = 0;
            ConnectDB objData = new ConnectDB();
            string strSQL = "";
            try
            {
                strSQL = "sp_GetUserID @UserName = '" + UserName + "'";
                objData.ExeReader(strSQL);
                if (objData.dataRead.Read())
                {
                    UserID = int.Parse(objData.dataRead["UserID"].ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
            return UserID;
        }    
        #endregion
        //Function Init Account
        #region Init user account
        public void InitAccount(Account acc) 
        {
            objData = new ConnectDB();
            string strSQLInInitAcount = "";
            try 
            {
                //InitAccount query
                strSQLInInitAcount = "sp_InitAccount  @UserID=" + acc.UserID +""
                                                   +",@Money ="+ acc.Money +""  
                                                   +",@bonus ="+ acc.Bonus +""
                                                   +",@GameID="+ acc.GameID +"";     
                //Execute
                objData.ExeNonQuery(strSQLInInitAcount);
            }   
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
        }
        #endregion

    }




}